// pages/daili/daili.js
var app = getApp();
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		// 此页面 页面内容距最顶部的距离
		error: '',
		height: app.globalData.height * 2 + 20,
		show: false,
		num1: 0,
		num2: 0,
		phone: 0,
		array: [],
		index: '',
		index2: '',
		allArr: [],
		newArr: [],
		isDis: true, //是否禁用
		newName: [],
		area_id: '',
		username: '',
		idea: '',
		advantage: '',
		sur: 0,
		imgUrl: config.imgUrl,
		newUrl: 'https://images.weigeee.com'
	},
	open() {
		this.setData({
			show: true
		})
	},
	close() {
		this.setData({
			show: false
		})
	},
	write1(e) {
		this.setData({
			num1: e.detail.value.length,
			idea: e.detail.value
		})
	},
	write2(e) {
		this.setData({
			num2: e.detail.value.length,
			advantage: e.detail.value
		})
	},
	wacthPhone(e) {
		this.setData({
			phone: e.detail.value
		})
	},

	wacthName(e) {
		console.log(e.detail.value)
		this.setData({
			username: e.detail.value
		})
	},

	// 提交
	submit() {
		let username = this.data.username
		let phone = this.data.phone
		let area_id = this.data.newArr[this.data.index2]
		if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) {
			wx.showToast({
				title: '请正确填写电话',
				icon: 'none'
			})
		} else {
			if (username && phone && area_id) {
				http.request({
					url: '/Investment/add',
					data: {
						username,
						phone,
						area_id: area_id.city_id
					},
					success: res => {
						console.log(res)
						if (res.code == 200) {
							wx.showModal({
								content: '稍后会有客服人员联系您，\r\n请保持电话通畅。',
								showCancel: false,
								confirmColor: '#EA5D56',
								success: function (e) {
									if (e.confirm) {
										wx.navigateBack({});
									}
								}
							})
						} else {
							wx.showModal({
								content: res.message+ ',无法继续提交,可申请其他地区代理',
								showCancel: false,
								confirmColor: '#EA5D56',
								success: function (e) {
									if (e.confirm) {
										
									}
								}
							})
						}
					}
				})
			} else {
				wx.showToast({
					title: '请输入完整信息',
					icon: 'none',
					mask: true
				})
			}
		}
	},


	// 地区
	//地区选择器：
	bindPickerChange: function (e) {
		console.log('picker发送选择改变，携带值为', e)
		this.setData({
			index: e.detail.value,
			isDis: false,
			index2: 0,
			newArr: this.data.allArr[e.detail.value],
		})
		let newName = []
		let sur = []
		this.data.newArr.forEach((item, index) => {
			console.log(item)
			newName.push(item.name)
			sur.push(item.number)
		})
		this.setData({
			newName,
			sur
		})
	},

	bindPickerChange2: function (e) {
		console.log('picker发送选择改变，携带值为', e)
		this.setData({
			index2: e.detail.value,
		})
	},

	// 短信验证码
	huoqu() {
		let obj = {
			phone: this.data.phone,
			client_id: wx.getStorageSync('user_id')
		}
		http.request({
			url: '/Investment/Investment_sms',
			data: obj,
			success: res => {
				console.log(res)
				if (res.code == 200) {
					wx.setStorageSync('code', res.ss)
					wx.showToast({
						title: '获取成功',
						icon: 'success',
						mask: true
					})
				}
			}
		})
	},

	// 获取招商区域
	diqu() {
		http.request({
			url: '/Investment/investment_address',
			success: res => {
				let array = []
				let allArr = []
				console.log(res)
				res.data.forEach((item, index) => {
					array.push(item.name)
					allArr.push(item.list)
				})
				this.setData({
					array,
					allArr
				})
			},
		})
	},

	// 判断是否为代理
	daili() {
		http.request({
			url: '/Investment/investment_is',
			data: {
				client_id: wx.getStorageSync('user_id')
			},
			success: res => {
				console.log(res)
				if (res.message == '该用户已经是商户') {
					wx.showToast({
						title: '您已是代理，无需重复申请',
						icon: 'none',
						duration: 1500,
						mask: true
					})
					setTimeout(() => {
						wx.switchTab({
							url: '/pages/my/my',
						})
					}, 1000)
				}
			}
		})
	},
	bindTextAreaBlur: function (e) {
		console.log(e.detail.value)
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		wx.removeStorageSync('code')
		this.daili()
		this.diqu()
		// zhenzisms.client.send(function (res) {
		//   console.log(res.data);
		// }, '13407133847', '验证码为:{code}');
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	}
})
